<%@page import="org.apache.tomcat.jni.Directory"%>
<%@page import="java.util.Enumeration"%>
<%@ page
	import="java.io.File,java.io.FilenameFilter,java.util.Arrays"%>
<%
/**
  * jQuery File Tree JSP Connector
  * Version 1.0
  * Copyright 2008 Joshua Gould
  * 21 April 2008
*/	
String baseDir = "/opt/apache-tomcat/Documents/";
String[] extensions;
if(request.getParameter("filterExtensions") != null){
    String filterExtensions = request.getParameter("filterExtensions").toLowerCase();
    extensions = filterExtensions.split("\\|");
} else {
    extensions = new String[0];
}

String dir = "C:/PDL/" + request.getParameter("dir");
boolean showFile = false;

if (dir == null) {
    return;
}

    if (dir.charAt(dir.length()-1) == '\\') {
    dir = dir.substring(0, dir.length()-1) + "/";
    } else if (dir.charAt(dir.length()-1) != '/') {
        dir += "/";
    }

    dir = java.net.URLDecoder.decode(dir, "UTF-8");	

if (new File(dir).exists()) {
            String[] files = new File(dir).list(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                            return name.charAt(0) != '.';
                }
            });
            Arrays.sort(files, String.CASE_INSENSITIVE_ORDER);
            out.print("<ul class=\"jqueryFileTree\" style=\"display: none;\">");
            // All dirs
            for (String file : files) {
                if (new File(dir, file).isDirectory()) {
                            if(request.getParameter("checkBoxes").equals("true")){
                                out.print("<li class=\"directory collapsed\"><div style=\"position:absolute;\"><input type=\"checkbox\" value=\"" + dir.replace("C:/PDL/","") + file + "\"></div><a href=\"#\" rel=\"" + dir.replace("C:/PDL/","") + file + "/\" onclick=\"window.currentDir = '"+ dir.replace("C:/PDL/","") + file+"';\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                                    + file + "</a></li>");
                            } else {
                                out.print("<li class=\"directory collapsed\"><a href=\"#\" rel=\"" + dir.replace("C:/PDL/","") + file + "/\" onclick=\"alert('test');\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                                    + file + "</a></li>");                                
                            }
                }
            }
            // All files
            for (String file : files) {
                if (!new File(dir, file).isDirectory()) {
                        if(extensions.length>0){
                            showFile = false;
                            for(String extension : extensions){
                                if(extension.equals(file.substring(file.lastIndexOf('.') + 1).toLowerCase())){
                                    showFile = true;
                                }
                            }
                            
                        } else {
                            showFile = true;
                        }
                        if(showFile==true){
                            int dotIndex = file.lastIndexOf('.');
                            String ext = dotIndex > 0 ? file.substring(dotIndex + 1) : "";
                            if(request.getParameter("checkBoxes").equals("true")){
                                out.print("<li class=\"file ext_" + ext + "\"><div style=\"position:absolute;\"><input type=\"checkbox\" value=\"" + dir.replace("C:/PDL/","") + file + "\"></div><a href=\"#\" rel=\"" + dir.replace("C:/PDL/","") + file + "\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                                    + file +  "</a></li>");
                            } else {
                                out.print("<li class=\"file ext_" + ext + "\"><a href=\"#\" rel=\"" + dir.replace("C:/PDL/","") + file + "\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                                    + file +  "</a></li>");
                            }
                        }
                        
                    }
            }
            out.print("</ul>");
}
%>